// pages/changeCard/changeCard.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    card: "",
    old_card: "",
    new_old_card: "", // 新签约卡号
    phone: "", // 手机号码
    code: "", // 验证码
    enterprise_name: "",
    facilitator_name: "",
    enterprise_professional_facilitator_id: "",
    editCode: false, //  获取验证
    code_show: '', //  倒计时展示
  },
  _input: function(e) {
    let {
      type
    } = e.currentTarget.dataset || {}, data = {};
    data[type] = e.detail.value || '';
    this.setData(data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    if (options.id) {
      let data = await wx.$api.getEfpDetail({
        enterprise_professional_facilitator_id: options.id
      });
      let {
        bank_code,
        cer_code,
        enterprise_name,
        facilitator_name,
        name
      } = data.data.data || {};
      this.setData({
        username: name,
        card: cer_code,
        old_card: bank_code,
        enterprise_name: enterprise_name,
        facilitator_name: facilitator_name,
        enterprise_professional_facilitator_id: options.id
      })
    }
  },
  // 跟换卡号
  async submit() {
    let {
      new_old_card,
      phone,
      code,
      enterprise_professional_facilitator_id
    } = this.data || {};
    let show_title = (msg) => {
      wx.$Toast(msg || '不能为空');
    }
    if (new_old_card == '') {
      show_title('签约卡号不能为空');
      return;
    }
    if (phone == '') {
      show_title('手机号不能为空');
      return;
    }
    if (!wx.$isMobile(phone)) {
      show_title('手机号格式不正确');
      return;
    }
    if (code == '') {
      show_title('请输入验证码');
      return;
    }
    let data = await wx.$api.modifyBankCode({
      bank_code: new_old_card,
      mobile: phone,
      v_code: code,
      enterprise_professional_facilitator_id: enterprise_professional_facilitator_id
    })
    let data_ = data.data || {};
    if (data_.code == 200) {
      show_title("更换成功");
      setTimeout(() => {
        let page = getCurrentPages()[getCurrentPages().length - 2];
        page.onLoad();
        wx.navigateBack({

        })
      }, 500)
    } else {
      show_title(data_.msg);
    }
    console.log(data, 'ssss')
  },
  // 获取验证码
  async getCode() {
    let self = this,
      code = 120;
    if (self.data.phone && wx.$isMobile(self.data.phone)) {
      let data = await wx.$api.sendCodes({
        mobile: self.data.phone
      });
      console.log('data', data)
      if (data.data.code == 200) {
        wx.$Toast("验证码已发送");
        self.setData({
          editCode: true,
          code_show: code + 's'
        })
        let T = setInterval(() => {
          code -= 1;
          self.setData({
            code_show: code + 's'
          })
          if (code <= 0) {
            clearInterval(T);
            self.setData({
              editCode: false
            })
          }
        }, 1000)
      } else {
        wx.$Toast(data.data.msg);
        self.setData({
          editCode: false,
        })
      }
    } else {
      wx.$Toast('请输入正确手机');
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})